$(function(){
    //刷新验证码
    $("#vCode").click(function(){
		$("#vCode").attr("src","/common/getVerifyCode.do?time="+ new Date().getTime());
	});
    //校验登录名
    $('#name').blur(function(){
        nameCheck();
    });
    //点击确定按钮触发事件
    $("#validate-num").click(function(){
        //名称的值
        var name=$('#name').val();
        //检查长度
        if(name.length == ""){
            $('#tipName').text('请输入登录名').addClass('tips').show();
            return false;
        }
        var vCode = $('#verify').val()||'';
        if(!vCode){
            $('#tipVCode').text('请输入验证码').addClass('tips').show();
            return false;
        }
        if(vCode.length!=4){
            $('#tipVCode').text('验证码输入错误').addClass('tips').show();
            return false;
        }
        $.ajax({
    		type: "POST",
    		url: "/common/checkVerifyCode.do",
    		data: {"vCode":vCode},
    		async: false,
    		dataType: "json",
    		success: function (data) {
    			if(data&&data.returnFlag == 'fail'){
    				  $('#tipVCode').text(data.msg).addClass('tips').show();
    		           return false;
    			}else{
    				//对用户名进行加密
    				var param=window.btoa(window.encodeURIComponent("loginName="+name));
    				window.location.href="changePassword.html?"+param;
                }
             }
         });
    });
});

//名称的检查,检查用户名是否存在
function nameCheck(){
	//名称的值
	var name=$('#name').val()||'';
	//检查长度
	if(name.length == ""){
		$('#tipName').text('请输入登录名').addClass('tips').show();
		return false;
	}
	var isRight = true;
	$.ajax({
		type: "POST",
		url: "/forget/userExist.do",
		data: {"loginName":name},
		async: false,
		dataType: "json",
		success: function (data) {
			  if(data&&data.returnFlag == 'fail'){
				$('#tipName').text(data.msg).addClass('tips').show();
				isRight= false;
			}else{
				$('#tipName').hide();
            }
         }
     });
     return isRight;
}
